package com.xebest.web.service.impl;  

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.xebest.common.enums.erp.sys.VenDelEnum;
import com.xebest.common.util.XeBaseUtil;
import com.xebest.util.seq.PurchSeqUtil;
import com.xebest.web.form.XeVenPurchProductForm;
import com.xebest.web.form.XeVenPurchSupAptitudeForm;
import com.xebest.web.form.XeVenPurchSupBankForm;
import com.xebest.web.form.XeVenPurchSupContractForm;
import com.xebest.web.form.XeVenPurchSupInfoForm;
import com.xebest.web.form.XeVenPurchSupLinkmanForm;
import com.xebest.web.mapper.read.XeVenPurchOrderReadMapper;
import com.xebest.web.mapper.read.XeVenPurchSupInfoReadMapper;
import com.xebest.web.mapper.write.XeVenPurchSupAptitudeWriteMapper;
import com.xebest.web.mapper.write.XeVenPurchSupBankWriteMapper;
import com.xebest.web.mapper.write.XeVenPurchSupContractWriteMapper;
import com.xebest.web.mapper.write.XeVenPurchSupInfoWriteMapper;
import com.xebest.web.mapper.write.XeVenPurchSupLinkmanWriteMapper;
import com.xebest.web.model.XeVenPurchSupAptitudeEntity;
import com.xebest.web.model.XeVenPurchSupBankEntity;
import com.xebest.web.model.XeVenPurchSupContractEntity;
import com.xebest.web.model.XeVenPurchSupInfoEntity;
import com.xebest.web.model.XeVenPurchSupLinkmanEntity;
import com.xebest.web.service.XeVenPurchSupplierService;
import com.xebest.web.view.XeVenPurchSupAptitudeView;
import com.xebest.web.view.XeVenPurchSupBankView;
import com.xebest.web.view.XeVenPurchSupContractView;
import com.xebest.web.view.XeVenPurchSupInfoView;
import com.xebest.web.view.XeVenPurchSupLinkmanView;

/**  
 * @desc: 供货商实现类
 * @author: 李书峰
 * @createTime: 2017年6月28日 下午7:13:41  
 * @history:  
 * @version: v1.0    
 */
@Service
public class XeVenPurchSupplierServiceImpl implements XeVenPurchSupplierService{

	private static final Logger log = LoggerFactory.getLogger(XeVenStoreRoleServiceImpl.class);//日志记录类

	@Autowired
	private XeVenPurchSupAptitudeWriteMapper xeVenPurchSupAptitudeWriteMapper; 
	@Autowired
	private XeVenPurchSupBankWriteMapper xeVenPurchSupBankWriteMapper;
	@Autowired
	private XeVenPurchSupLinkmanWriteMapper xeVenPurchSupLinkmanWriteMapper;
	@Autowired
	private XeVenPurchSupContractWriteMapper xeVenPurchSupContractWriteMapper;
	@Autowired
	private XeVenPurchSupInfoWriteMapper xeVenPurchSupInfoWriteMapper;
	@Autowired
	private XeVenPurchSupInfoReadMapper xeVenPurchSupInfoReadMapper;
	@Autowired
	private XeVenPurchOrderReadMapper xeVenPurchOrderReadMapper;

	/**
	 * 新增供货商
	 * @author: 李书峰
	 * @createTime: 2017年6月28日 下午7:41:40  
	 * @history:  
	 * @param xeVenPurchSupInfoForm
	 * @return
	 * @throws Exception int
	 */
	@Override
	@Transactional(value="writeTransactionManager")
	public int insertSupplier(XeVenPurchSupInfoForm xeVenPurchSupInfoForm)
			throws Exception {
		//插入结果
		int resultSave = 0;
		//判空
		if (null == xeVenPurchSupInfoForm) 
			return resultSave;
		
		//插入供货商结果
		int supplierReult = 1;
		//插入供货商
		XeVenPurchSupInfoEntity supplier = null ;
		supplier = JSONObject.parseObject(JSONObject.toJSONString(xeVenPurchSupInfoForm), XeVenPurchSupInfoEntity.class);
		Date date = new Date();
		supplier.setModifyDate(date);
		supplier.setCreateDate(date);
		supplier.setCreateUser(xeVenPurchSupInfoForm.getModifyUser());
		supplier.setDelFlag(VenDelEnum.NO.getValue());
		supplier.setVersion(0);
		supplierReult = xeVenPurchSupInfoWriteMapper.insertSupInfo(supplier);
		
		//插入联系人
		int supplierLinkMan = 1;
		List<XeVenPurchSupLinkmanForm> linkManList = xeVenPurchSupInfoForm.getLinkmanList();
		if(null != linkManList && linkManList.size() > 0 && supplierReult > 0 ){
			List<XeVenPurchSupLinkmanEntity> linkManEntityList = new ArrayList<XeVenPurchSupLinkmanEntity>();
			for(XeVenPurchSupLinkmanForm form:linkManList){
				form.setLinkmanCode(PurchSeqUtil.seqUtils.createLinkmanCode());
				form.setSupplierCode(xeVenPurchSupInfoForm.getSupplierCode());
				XeVenPurchSupLinkmanEntity linkMan = JSONObject.parseObject(JSONObject.toJSONString(form), XeVenPurchSupLinkmanEntity.class);
				linkManEntityList.add(linkMan);
			}
			supplierLinkMan = xeVenPurchSupLinkmanWriteMapper.insertSupLinkmanBatch(linkManEntityList);
		}
		
		//插入合同
		int supplierContact = 1;
		List<XeVenPurchSupContractForm> contractList = xeVenPurchSupInfoForm.getContractList();
		if(null != contractList && contractList.size() > 0 && supplierReult > 0){
			List<XeVenPurchSupContractEntity> contractEntityList = new ArrayList<XeVenPurchSupContractEntity>();
			String contractCode = PurchSeqUtil.seqUtils.createContractCode();
			for(XeVenPurchSupContractForm form:contractList){
				form.setContractCode(contractCode);
				form.setFileCode(PurchSeqUtil.seqUtils.createFileCode(contractCode));
				form.setSupplierCode(xeVenPurchSupInfoForm.getSupplierCode());
				XeVenPurchSupContractEntity contract = JSONObject.parseObject(JSONObject.toJSONString(form), XeVenPurchSupContractEntity.class);
				contractEntityList.add(contract);
			}
			supplierContact = xeVenPurchSupContractWriteMapper.insertContractBatch(contractEntityList);
		}
		
		//插入资质证明
		int supplierAptitude = 1;
		List<XeVenPurchSupAptitudeForm> aptitudeList = xeVenPurchSupInfoForm.getAptitudeList();
		if(null != aptitudeList && aptitudeList.size() > 0 && supplierReult > 0){
			List<XeVenPurchSupAptitudeEntity> detailEntityList = new ArrayList<XeVenPurchSupAptitudeEntity>();
			for(XeVenPurchSupAptitudeForm form:aptitudeList){
				form.setApId(PurchSeqUtil.seqUtils.createAptitudeCode());
				form.setSupplierCode(xeVenPurchSupInfoForm.getSupplierCode());
				XeVenPurchSupAptitudeEntity aptitude = JSONObject.parseObject(JSONObject.toJSONString(form), XeVenPurchSupAptitudeEntity.class);
				detailEntityList.add(aptitude);
			}
			supplierAptitude = xeVenPurchSupAptitudeWriteMapper.insertAptitudeBatch(detailEntityList);
		}
		
		//插入开户行
		int supplierBank = 1;
		if(supplierReult > 0){
			XeVenPurchSupBankForm bank = xeVenPurchSupInfoForm.getBank();
			bank.setSupplierCode(xeVenPurchSupInfoForm.getSupplierCode());
			bank.setBankId(PurchSeqUtil.seqUtils.createBankCode());
			XeVenPurchSupBankEntity bankEntity = JSONObject.parseObject(JSONObject.toJSONString(bank), XeVenPurchSupBankEntity.class);
			supplierBank = xeVenPurchSupBankWriteMapper.insertBank(bankEntity);
		}
		
		resultSave = supplierReult > 0 && supplierLinkMan > 0 && supplierContact > 0
				 && supplierAptitude > 0 && supplierBank > 0 ? 1 : 0; 
		log.debug("插入供货商主表结果："+supplierReult+"，插入联系人结果："+supplierLinkMan +"，插入合同结果："
				 +supplierContact +"，插入资质结果："+supplierAptitude +"，插入开户行结果："
				+supplierBank +"，插入供货商结果："+resultSave);
		//插入失败对主表删除
		if(0 == resultSave){
			supplier.setDelFlag(VenDelEnum.YES.getValue());
			supplierReult = xeVenPurchSupInfoWriteMapper.updateSupInfo(supplier);
		}
		return resultSave;   
	}
	
	/**
	 * 编辑供货商
	 * @author: 李书峰
	 * @createTime: 2017年6月28日 下午7:41:40  
	 * @history:  
	 * @param xeVenPurchSupInfoForm
	 * @return
	 * @throws Exception int
	 */
	@Override
	@Transactional(value="writeTransactionManager")
	public int updateSupplier(XeVenPurchSupInfoForm xeVenPurchSupInfoForm)
			throws Exception {
		//更新结果
		int resultSave = 0;
		//判空
		if (null == xeVenPurchSupInfoForm) 
			return resultSave;
		
		Date date = new Date();
		String userName = xeVenPurchSupInfoForm.getModifyUser();
		//更新供货商结果
		int supplierReult = 1;
		//更新供货商
		XeVenPurchSupInfoEntity supplier = null ;
		supplier = JSONObject.parseObject(JSONObject.toJSONString(xeVenPurchSupInfoForm), XeVenPurchSupInfoEntity.class);
		supplier.setModifyDate(date);
		supplier.setVersion(xeVenPurchSupInfoForm.getVersion());
		supplierReult = xeVenPurchSupInfoWriteMapper.updateSupInfo(supplier);
		resultSave = supplierReult;
		
		//更新联系人
		int supplierLinkMan = 1;
		List<XeVenPurchSupLinkmanForm> linkManList = xeVenPurchSupInfoForm.getLinkmanList();
		if(null != linkManList && linkManList.size() > 0 && supplierReult > 0){
			List<XeVenPurchSupLinkmanEntity> linkManEntityList = new ArrayList<XeVenPurchSupLinkmanEntity>();
			for(XeVenPurchSupLinkmanForm form:linkManList){
				form.setSupplierCode(xeVenPurchSupInfoForm.getSupplierCode());
				form.setLinkmanCode(PurchSeqUtil.seqUtils.createLinkmanCode());
				XeVenPurchSupLinkmanEntity linkMan = JSONObject.parseObject(JSONObject.toJSONString(form), XeVenPurchSupLinkmanEntity.class);
				linkManEntityList.add(linkMan);
			}
			XeVenPurchSupLinkmanEntity linkManEntity = new XeVenPurchSupLinkmanEntity();
			linkManEntity.setDelFlag(VenDelEnum.YES.getValue());
			linkManEntity.setSupplierCode(xeVenPurchSupInfoForm.getSupplierCode());
			linkManEntity.setModifyDate(date);
			linkManEntity.setModifyUser(userName);
			xeVenPurchSupLinkmanWriteMapper.updateLinkman(linkManEntity);
			supplierLinkMan = xeVenPurchSupLinkmanWriteMapper.insertSupLinkmanBatch(linkManEntityList);
		}
		
		//更新合同
		int supplierContact = 1;
		List<XeVenPurchSupContractForm> contractList = xeVenPurchSupInfoForm.getContractList();
		if(null != contractList && contractList.size() > 0 && supplierReult > 0 ){
			List<XeVenPurchSupContractEntity> contractEntityList = new ArrayList<XeVenPurchSupContractEntity>();
			String contractCode = PurchSeqUtil.seqUtils.createContractCode();
			for(XeVenPurchSupContractForm form:contractList){
				form.setContractCode(contractCode);
				form.setSignMan(form.getModifyUser());
				form.setFileCode(PurchSeqUtil.seqUtils.createFileCode(contractCode));
				form.setSupplierCode(xeVenPurchSupInfoForm.getSupplierCode());
				XeVenPurchSupContractEntity contract = JSONObject.parseObject(JSONObject.toJSONString(form), XeVenPurchSupContractEntity.class);
				contractEntityList.add(contract);
			}
			XeVenPurchSupContractEntity contractEntity = new XeVenPurchSupContractEntity();
			contractEntity.setDelFlag(VenDelEnum.YES.getValue());
			contractEntity.setSupplierCode(xeVenPurchSupInfoForm.getSupplierCode());
			contractEntity.setModifyTime(date);
			contractEntity.setModifyUser(userName);
			xeVenPurchSupContractWriteMapper.updateContract(contractEntity);
			supplierContact = xeVenPurchSupContractWriteMapper.insertContractBatch(contractEntityList);
		}
		
		//更新资质证明
		int supplierAptitude = 1;
		//删除旧的资质证明
		if(!new Integer(2).equals(xeVenPurchSupInfoForm.getUpdateAptitude())){
			XeVenPurchSupAptitudeEntity aptitudeEntity = new XeVenPurchSupAptitudeEntity();
			aptitudeEntity.setDelFlag(VenDelEnum.YES.getValue());
			aptitudeEntity.setSupplierCode(xeVenPurchSupInfoForm.getSupplierCode());
			aptitudeEntity.setModifyTime(date);
			aptitudeEntity.setModifyUser(userName);
			xeVenPurchSupAptitudeWriteMapper.updateAptitude(aptitudeEntity);
		}
		if(null != xeVenPurchSupInfoForm.getUpdateAptitude()
				&& new Integer(1).equals(xeVenPurchSupInfoForm.getUpdateAptitude())){
			List<XeVenPurchSupAptitudeForm> aptitudeList = xeVenPurchSupInfoForm.getAptitudeList();
			if(null != aptitudeList && aptitudeList.size() > 0 && supplierReult > 0 ){
				List<XeVenPurchSupAptitudeEntity> detailEntityList = new ArrayList<XeVenPurchSupAptitudeEntity>();
				for(XeVenPurchSupAptitudeForm form:aptitudeList){
					form.setApId(PurchSeqUtil.seqUtils.createAptitudeCode());
					form.setSupplierCode(xeVenPurchSupInfoForm.getSupplierCode());
					XeVenPurchSupAptitudeEntity aptitude = JSONObject.parseObject(JSONObject.toJSONString(form), XeVenPurchSupAptitudeEntity.class);
					detailEntityList.add(aptitude);
				}
				supplierAptitude = xeVenPurchSupAptitudeWriteMapper.insertAptitudeBatch(detailEntityList);
			}
		}
		
		//更新开户行
		int supplierBank = 1;
		XeVenPurchSupBankForm bank = xeVenPurchSupInfoForm.getBank();
		if( null != bank 
//				&& StringUtils.isNotBlank(bank.getAccBankCode()) 
				&& supplierReult > 0 ){
			bank.setSupplierCode(xeVenPurchSupInfoForm.getSupplierCode());
			bank.setBankId(PurchSeqUtil.seqUtils.createBankCode());
			XeVenPurchSupBankEntity bankEntity = JSONObject.parseObject(JSONObject.toJSONString(bank), XeVenPurchSupBankEntity.class);
			supplierBank = xeVenPurchSupBankWriteMapper.updateBank(bankEntity);
		}
		
//		resultSave = supplierReult > 0 && supplierLinkMan > 0 && supplierContact > 0
//				 && supplierAptitude > 0 && supplierBank > 0 ? 1 : 0; 
		log.debug("更新供货商主表结果："+supplierReult+"，更新联系人结果："+supplierLinkMan +"，更新合同结果："
				 +supplierContact +"，更新资质结果："+supplierAptitude +"，更新开户行结果："
				+supplierBank +"，更新供货商结果："+resultSave);
		
		return resultSave;  
	}
	
	/**
	 * 删除供货商
	 * @author: 李书峰
	 * @createTime: 2017年6月28日 下午7:41:40  
	 * @history:  
	 * @param xeVenPurchSupInfoForm
	 * @return
	 * @throws Exception int
	 */
	@Override
	@Transactional(value="writeTransactionManager")
	public int deleteSupplier(XeVenPurchSupInfoForm xeVenPurchSupInfoForm)
			throws Exception {
		//删除结果
		int resultSave = 0;
				
		//判空
		if (null == xeVenPurchSupInfoForm) 
			return resultSave;
		
		Date date = new Date();
//		String userName = xeVenPurchSupInfoForm.getModifyUser();
//		String supCode = xeVenPurchSupInfoForm.getSupplierCode();
		//更新供货商
		XeVenPurchSupInfoEntity supplier = null ;
		supplier = JSONObject.parseObject(JSONObject.toJSONString(xeVenPurchSupInfoForm), XeVenPurchSupInfoEntity.class);
		supplier.setModifyDate(date);
		int supplierReult = xeVenPurchSupInfoWriteMapper.updateSupInfo(supplier);
		resultSave = supplierReult;
		
/*		//（勿删）
 		//删除联系人
		XeVenPurchSupLinkmanEntity linkManEntity = new XeVenPurchSupLinkmanEntity();
		linkManEntity.setDelFlag(VenDelEnum.YES.getValue());
		linkManEntity.setSupplierCode(supCode);
		linkManEntity.setModifyDate(date);
		linkManEntity.setModifyUser(userName);
		int supplierLinkMan = xeVenPurchSupLinkmanWriteMapper.updateLinkman(linkManEntity);
		
		//删除合同
		XeVenPurchSupContractEntity contractEntity = new XeVenPurchSupContractEntity();
		contractEntity.setDelFlag(VenDelEnum.YES.getValue());
		contractEntity.setSupplierCode(supCode);
		contractEntity.setModifyTime(date);
		contractEntity.setModifyUser(userName);
		int supplierContact = xeVenPurchSupContractWriteMapper.updateContract(contractEntity);
		
		//更新资质证明
		XeVenPurchSupAptitudeEntity aptitudeEntity = new XeVenPurchSupAptitudeEntity();
		aptitudeEntity.setDelFlag(VenDelEnum.YES.getValue());
		aptitudeEntity.setSupplierCode(supCode);
		aptitudeEntity.setModifyTime(date);
		aptitudeEntity.setModifyUser(userName);
		int supplierAptitude = xeVenPurchSupAptitudeWriteMapper.updateAptitude(aptitudeEntity);
		
		//更新开户行
		XeVenPurchSupBankEntity bankEntity = new XeVenPurchSupBankEntity();
		bankEntity.setDelFlag(VenDelEnum.YES.getValue());
		bankEntity.setSupplierCode(supCode);
		bankEntity.setModifyDate(date);
		bankEntity.setModifyUser(userName);
		int supplierBank = xeVenPurchSupBankWriteMapper.updateBank(bankEntity);*/
		
/*		resultSave = supplierReult > 0 && supplierLinkMan > 0 && supplierContact > 0
				 && supplierAptitude > 0 && supplierBank > 0 ? 1 : 0; 
		log.debug("更新供货商主表结果："+supplierReult+"，更新联系人结果："+supplierLinkMan +"，更新合同结果："
				 +supplierContact +"，更新资质结果："+supplierAptitude +"，更新开户行结果："
				+supplierBank +"，更新供货商结果："+resultSave);*/
		
		return resultSave;  
		
	}

	/**
	 * 查询供货商列表
	 * @author: 李书峰
	 * @createTime: 2017年6月28日 下午7:43:15  
	 * @history:  
	 * @param xeVenPurchSupInfoForm
	 * @return List<XeVenPurchSupInfoView>
	 */
	@Override
	public List<XeVenPurchSupInfoView> querySupplierList( XeVenPurchSupInfoForm xeVenPurchSupInfoForm ){
		//判空
		if (null == xeVenPurchSupInfoForm) 
			return null;
		//查询供货商
		XeVenPurchSupInfoEntity supplier = JSONObject.parseObject(JSONObject.toJSONString(xeVenPurchSupInfoForm), XeVenPurchSupInfoEntity.class);
		List<XeVenPurchSupInfoView> supList = xeVenPurchSupInfoReadMapper.selectSupplier(supplier);
		return supList;  
	}
	
	/**
	 * 供货商搜索列表
	 * @author: 李书峰
	 * @createTime: 2017年6月28日 下午7:43:15  
	 * @history:  
	 * @param xeVenPurchSupInfoForm
	 * @return List<XeVenPurchSupInfoView>
	 */
	@Override
	public List<Map<String,Object>> searchSupplierList( XeVenPurchSupInfoForm xeVenPurchSupInfoForm ){
		//判空
		if (null == xeVenPurchSupInfoForm) 
			return null;
		//查询供货商
		XeVenPurchSupInfoEntity supplier = JSONObject.parseObject(JSONObject.toJSONString(xeVenPurchSupInfoForm), XeVenPurchSupInfoEntity.class);
//		supplier.setUserCode("SH170712925781");
		List<Map<String,Object>> supList = xeVenPurchSupInfoReadMapper.searchSupplierList(supplier);
		return supList;  
	}

	/**
	 * 查询供货商详情
	 * @author: 李书峰
	 * @createTime: 2017年6月28日 下午7:43:36  
	 * @history:  
	 * @param xeVenPurchSupInfoForm
	 * @return XeVenPurchSupInfoView
	 */
	@Override
	public XeVenPurchSupInfoView querySupplierDetail(XeVenPurchSupInfoForm xeVenPurchSupInfoForm) {
		//判空
		if (null == xeVenPurchSupInfoForm) 
		return null;  
		//查询供货商主表
		XeVenPurchSupInfoEntity supplier = JSONObject.parseObject(JSONObject.toJSONString(xeVenPurchSupInfoForm), XeVenPurchSupInfoEntity.class);
		List<XeVenPurchSupInfoView> supList = xeVenPurchSupInfoReadMapper.selectSupplierDetail(supplier);
		XeVenPurchSupInfoView supInfo = null;
		if(null != supList && supList.size() > 0 ){
			supInfo = supList.get(0);
			String supCode = xeVenPurchSupInfoForm.getSupplierCode();
			String userCode = xeVenPurchSupInfoForm.getUserCode();
			
			XeVenPurchSupLinkmanEntity linkMan = new XeVenPurchSupLinkmanEntity();
			linkMan.setSupplierCode(supCode);
			linkMan.setUserCode(userCode);
			
			XeVenPurchSupContractEntity contract = new XeVenPurchSupContractEntity();
			contract.setSupplierCode(supCode);
			contract.setUserCode(userCode);
			
			XeVenPurchSupAptitudeEntity aptitude = new XeVenPurchSupAptitudeEntity();
			aptitude.setSupplierCode(supCode);
			aptitude.setUserCode(userCode);
			
			XeVenPurchSupBankEntity bank = new XeVenPurchSupBankEntity();
			bank.setSupplierCode(supCode);
			bank.setUserCode(userCode);
			
			List<XeVenPurchSupLinkmanView> linkmanList = xeVenPurchSupInfoReadMapper.selectLinkman(linkMan);
			List<XeVenPurchSupContractView> contractList = xeVenPurchSupInfoReadMapper.selectContract(contract);
			List<XeVenPurchSupAptitudeView> aptitudeList = xeVenPurchSupInfoReadMapper.selectAptitude(aptitude);
			List<XeVenPurchSupBankView> bankList = xeVenPurchSupInfoReadMapper.selectBank(bank);
			
			if(null != linkmanList && linkmanList.size() > 0 ){
				for(XeVenPurchSupLinkmanView link : linkmanList ){
					String provinceName = null != link.getProvinceId()?XeBaseUtil.baseUtil.getAreaName(link.getProvinceId()):null;
					String cityName = null != link.getCityId()?XeBaseUtil.baseUtil.getAreaName(link.getCityId()):null;;
					String areaName = null != link.getAreaId()?XeBaseUtil.baseUtil.getAreaName(link.getAreaId()):null;;
					link.setProvinceName(provinceName);
					link.setCityName(cityName);
					link.setAreaName(areaName);
				}
			}
			
			supInfo.setLinkmanList(linkmanList);
			supInfo.setContractList(contractList);
			supInfo.setAptitudeList(aptitudeList);
			XeVenPurchSupBankView bankView = null;
			if( null != bankList && bankList.size() > 0){
				bankView = bankList.get(0);
			}
			supInfo.setBank(bankView);
		}
		return supInfo;
		
	}

	/**
	 * 查询合同列表
	 * @author: 李书峰
	 * @createTime: 2017年6月28日 下午7:56:28  
	 * @history:  
	 * @param xeVenPurchSupInfoForm
	 * @return List<XeVenPurchSupContractView>
	 */
	@Override
	public List<XeVenPurchSupContractView> queryContractList(
			XeVenPurchSupInfoForm xeVenPurchSupInfoForm) {
		//判空
		if (null == xeVenPurchSupInfoForm) 
		return null; 
		//查询合同列表
		XeVenPurchSupContractEntity contract = new XeVenPurchSupContractEntity();
		contract.setSupplierCode(xeVenPurchSupInfoForm.getSupplierCode());
		contract.setUserCode(xeVenPurchSupInfoForm.getUserCode());
		List<XeVenPurchSupContractView> viewList = xeVenPurchSupInfoReadMapper.selectContract(contract);
		List<XeVenPurchSupContractView> contractList = new ArrayList<XeVenPurchSupContractView>();
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String,Object> fileMap = new HashMap<String,Object>();
		if( null != viewList && viewList.size() > 0){
			
			//如果签订历史所有图片要一次带出的话如下方法（为防止产品需求变更，此处勿删）
			//合同map
			for(XeVenPurchSupContractView view : viewList){
				map.put(view.getContractCode(), view.getContractCode());
			}
			//合同文件map
			for(XeVenPurchSupContractView view : viewList){
				for(Map.Entry<String,Object> mm : map.entrySet()){
					if(view.getContractCode().equals(mm.getKey())){
						List<String> fileList = (List<String>)fileMap.get(mm.getValue().toString());
						if(null == fileList || 0==fileList.size() ){
							fileList = new ArrayList<String>();
						}
						fileList.add(view.getContractUrl());
						fileMap.put(mm.getKey(), fileList);
						continue;
					}
				}
			}
			//返回前端list
			for(XeVenPurchSupContractView view : viewList){
				for(Map.Entry<String,Object> mm : fileMap.entrySet()){
					if(mm.getKey().equals(view.getContractCode())){
						List<String> fileList = (List<String>)mm.getValue();
						view.setFileList(fileList);
						contractList.add(view);
//						fileMap.remove(mm.getKey());
						continue;
					}
				}
			}
			/*//如果签订历史图片不用一次带出的话用以下方法（产品需求随时切换，此处勿删）
			//合同文件map
			for(XeVenPurchSupContractView view : viewList){
				if(null != map.get(view.getContractCode())){
					continue;
				}
				map.put(view.getContractCode(), view);
			}
			for(Map.Entry<String,Object> mm : map.entrySet()){
				XeVenPurchSupContractView view = (XeVenPurchSupContractView)mm.getValue();
				contractList.add(view);
			}*/
		}
		return contractList;  
	}
	
	/**
	 * 查询合同文件列表
	 * @author: 李书峰
	 * @createTime: 2017年6月28日 下午7:56:28  
	 * @history:  
	 * @param xeVenPurchSupInfoForm
	 * @return List<XeVenPurchSupContractView>
	 */
	@Override
	public List<String> queryContractFileList(
			XeVenPurchSupInfoForm xeVenPurchSupInfoForm) {
		//判空
		if (null == xeVenPurchSupInfoForm) 
		return null; 
		//查询合同列表
		XeVenPurchSupContractEntity contract = new XeVenPurchSupContractEntity();
		contract.setSupplierCode(xeVenPurchSupInfoForm.getSupplierCode());
		contract.setUserCode(xeVenPurchSupInfoForm.getUserCode());
		List<XeVenPurchSupContractView> viewList = xeVenPurchSupInfoReadMapper.selectContract(contract);
		List<String> contractList = new ArrayList<String>();
		if( null != viewList && viewList.size() > 0){
			//返回前端list
			for(XeVenPurchSupContractView view : viewList){
				contractList.add(view.getContractUrl());
			}
		}
		return contractList;  
	}
	  
	/**
	 * 查询供货商商品购买记录列表
	 * @author: 李书峰
	 * @createTime: 2017年6月28日 下午7:50:43  
	 * @history:  
	 * @param xeVenPurchSupInfoForm
	 * @return List<XeVenPurchSupProductView>
	 */
	@Override
	public List<Map<String,Object>> queryProductList(XeVenPurchProductForm xeVenPurchProductForm) {
		//判空
		if (null == xeVenPurchProductForm)
		return null; 
		//组装参数
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userCode", xeVenPurchProductForm.getUserCode());
		if(null != xeVenPurchProductForm.getSupplierName()){
			map.put("supplierName", xeVenPurchProductForm.getSupplierName());
		}
		if( null != xeVenPurchProductForm.getProName()){
			map.put("proName", xeVenPurchProductForm.getProName());
		}
		if(null != xeVenPurchProductForm.getProNo()){
			map.put("proNo", xeVenPurchProductForm.getProNo());
		}
		map.put("categoryCode", xeVenPurchProductForm.getCategoryCode());
		List<Map<String,Object>> products = xeVenPurchOrderReadMapper.queryAllProductList(map);
		//List<XeVenPurchSupProductView> productList = xeVenPurchSupInfoReadMapper.selectProduct(product);
		return products;  
	}

	/**
	 * 查询api所需供货商code和name列表
	 * @author: 李书峰
	 * @createTime: 2017年7月11日 上午10:19:10  
	 * @history:  
	 * @param supplierEntity
	 * @return List<XeVenPurchSupInfoView>
	 */  
	@Override
	public List<XeVenPurchSupInfoView> querySupplierCodes(
			XeVenPurchSupInfoEntity supplierEntity) {
		return xeVenPurchSupInfoReadMapper.selectSupplierCodes(supplierEntity);  
	}

	/**
	 * 批量更新供货商
	 * @author: 李书峰
	 * @createTime: 2017年6月28日 下午7:41:40  
	 * @history:  
	 * @param xeVenPurchSupInfoForm
	 * @return
	 * @throws Exception int
	 */
	@Override
	public Map<String, Object> updateSupplierList(List<XeVenPurchSupInfoForm> formList) {
		Map<String, Object> map = new HashMap<String, Object>();  
		int resultInt = 0;
		StringBuilder msgBuilder = new StringBuilder("失败供货商code如下：");
		if( null == formList || formList.size() == 0){
			map.put("msg", "失败原因为未选中任何数据");
			map.put("successSize", 0);
			map.put("failSize", 0);
			map.put("totalSize", 0);
			return map;  
		}
		for(XeVenPurchSupInfoForm form : formList){
			XeVenPurchSupInfoEntity supplier = JSONObject.parseObject(JSONObject.toJSONString(form), XeVenPurchSupInfoEntity.class);
			supplier.setModifyDate(new Date());
			int updateInt = xeVenPurchSupInfoWriteMapper.updateSupInfo(supplier);
			if(updateInt == 0){
				msgBuilder.append(form.getSupplierCode()).append(",");
			}
			resultInt = resultInt + updateInt;
		}
		int failInt = formList.size() - resultInt;
		map.put("msg", msgBuilder.substring(0, msgBuilder.length()-1).toString());
		map.put("successSize", resultInt);
		map.put("failSize", failInt);
		map.put("totalSize", formList.size());
		return map;  
		
	}

	 /**
     * 查询采购供货商信息,支付中心-结算管理-结算白名单使用
     * @author: 李贝贝
     * @createTime: 2018年5月7日 下午2:14:22  
     * @history:  
     * @param supplier
     * @return List<XeVenPurchSupInfoView>
     */
   public List<XeVenPurchSupInfoView> listPurchSupInfo(XeVenPurchSupInfoEntity supplier){
	   return xeVenPurchSupInfoReadMapper.listPurchSupInfo(supplier);
   }
	
}
  
